<template>
  <div class="trans">
    <ul ref="transUl">
      <li class="trans-li">横向滚动</li>
      <li class="trans-li">横向滚动</li>
      <li class="trans-li">横向滚动</li>
      <li class="trans-li">横向滚动</li>
      <li class="trans-li">横向滚动</li>
      <li class="trans-li">横向滚动</li>
    </ul>
  </div>
</template>
<script>
import BScroll from 'better-scroll'
export default {
  mounted () {
    this.$nextTick(() => {
      this._initScroll()
    })
  },
  methods: {
    _initScroll () {
      // 动态计算ul的宽度
      const ul = this.$refs.transUl
      const lis = ul.getElementsByClassName('trans-li')
      const count = lis.length
      const liWidth = lis[0].clientWidth
      ul.style.width = liWidth * count + 'px'
      new BScroll('.trans', {
        scrollX: true, // 水平滑动
        probeType: 2,
      })
    }
  }
}
</script>
<style scoped>
.trans {
  width: 375px;
  overflow: hidden;
}
.trans ul {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.trans ul li {
  height: 100px;
  width: 150px;
  line-height: 100px;
  background-color: pink;
  border-right: 2px solid #fff;
  text-align: center;
}
</style>